# -*- coding:utf-8 -*-

"""
@author: fanqc
@file: util.py
@time: 2023/2/15 12:44
"""
import difflib
import os
import re
from typing import Dict

from exceptions import RunException


def convert_path(path: str) -> str:
    seps = r'\/'
    sep_other = seps.replace(os.sep, '')
    return path.replace(sep_other, os.sep) if sep_other in path else path


def is_in(full_str, sub_str):
    if re.findall(sub_str, full_str):
        return True
    else:
        return False


def max_index(list_num: list):
    return list_num.index(max(list_num))


def match_str_similarity(str1, str2):
    return round(difflib.SequenceMatcher(None, str1, str2).quick_ratio(), 2)


def remove_suff_af(str1: str, from_str):
    return str1.replace(str1[str1.rfind(from_str):], '') + from_str


def remove_suff(str1: str, from_str):
    return str1.replace(str1[str1.rfind(from_str):], '')


def remove_in(str1: str, list_str):
    for ss in list_str:
        if ss in str1:
            str1.replace(ss, '')
    return str1


def get_longest_name(str1: str):
    # 先过滤【】中无效词
    t = count_in(str1, '【')
    t_str1 = str1
    for i in range(t):
        str1 = deleteByStartAndEnd(str1, '【', '】')
    # 获取最长子词
    if ' ' in str1:
        str1 = max(str1.split(' '), key=len, default=t_str1[2:15])
        if len(str1) > 20:
            return str1[:20]
        elif len(str1) < 10:
            return t_str1[:20]
        else:
            return str1
    return str1[:20]


def get_search_keys(jd_sku_name: str, range_int=10):
    # 先过滤【】中无效词
    t = count_in(jd_sku_name, '【')
    for i in range(t):
        jd_sku_name = deleteByStartAndEnd(jd_sku_name, '【', '】')
    # 再替换 ''空格
    jd_sku_name = jd_sku_name.replace(' ', '')
    length = len(jd_sku_name)
    if length < range_int:
        raise RunException('切割范围不能大于原字符串长度')
    temp = length / range_int
    if temp > float(int(temp)):
        count = int(temp) + 1
    else:
        count = int(length / range_int)

    ll = []
    for i in range(count):
        if i < count - 2:
            ll.append(jd_sku_name[i * range_int: (i + 1) * range_int])
        elif i + 2 == count:
            ll.append(jd_sku_name[i * range_int: length])
    return ll


def count_in(str_source: str, str1: str):
    return int((len(str_source) - len(str_source.replace(str1, ""))) / len(str1))


def deleteInvalidWord(sku_name):
    t = count_in(sku_name, '【')
    for i in range(t):
        sku_name = deleteByStartAndEnd(sku_name, '【', '】')
    return sku_name


def deleteByStartAndEnd(s, start, end):
    try:
        x1 = s.index(start)
        x2 = s.index(end) + len(end)
        x3 = s[x1:x2]
        return s.replace(x3, "")
    except:
        return s


def get_inner_ip():
    import socket
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(('8.8.8.8', 80))
    ip = s.getsockname()[0]
    s.close()
    return ip


ll = ['官方旗舰店', '旗舰店', '专卖店', '专营店', '京东自营旗舰店', '京东自营店', '自营店']


def filter_shop_name(shop_name):
    return remove_in(shop_name, ll)


def str_to_dict(str_source: str) -> Dict:
    """
    字符串转字典（只支持单层结构）
    :param str_source: 字典格式字符串
    :return:
    """
    dict_result: dict = eval(str_source)
    return dict_result


if __name__ == '__main__':
    print(get_search_keys('【东阿阿胶官方旗 舰店】红标阿胶片240g×2礼盒装可熬阿胶糕固元膏阿胶块增强免疫缓解疲劳送礼礼盒'))
